package com.uchedao.seller.ui.car.publish;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.utils.L;
import com.uchedao.seller.R;
import com.uchedao.seller.ui.car.base.BaseActivity;
import com.uchedao.seller.ui.car.adapter.CarPhotoPagerAdapter;
import com.uchedao.seller.widget.HackyViewPager;
import com.uchedao.seller.widget.dialog_for_ios.ZAlertDialog;

import java.util.ArrayList;

/**
 * 图片预览
 */
public class ImagePagerActivity extends BaseActivity implements OnClickListener {

	private String title;

	private TextView titleTv;

	private ArrayList<String> pic_path_list;

	private HackyViewPager viewPager;

	private CarPhotoPagerAdapter adapter;

	private boolean modified;// 是否有修改

	private String nowUrl = "";

	@Override
	protected String getActivityTag() {
		return "ImagePagerActivity";
	}

	@Override
	protected int getLayoutResID() {
		return R.layout.activity_image_pager;
	}

	@Override
	protected void initView() {

		Intent intent = getIntent();
		title = intent.getStringExtra("title");
		pic_path_list = intent.getStringArrayListExtra("pic_path_list");
		nowUrl = pic_path_list.get(0);

		initAll();

	}

	@Override
	protected void initData() {

	}

	@Override
	protected void initListener() {

	}

	private void initAll() {
		findViewById(R.id.title_back).setOnClickListener(this);
		setPagerTitle(0);
		TextView editTv = (TextView) findViewById(R.id.update_db);
		editTv.setVisibility(View.VISIBLE);
		editTv.setText(R.string.delete);
		editTv.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.delete_icon,
				0, 0, 0);
		editTv.setOnClickListener(this);

		viewPager = (HackyViewPager) findViewById(R.id.pager);
		adapter = new CarPhotoPagerAdapter(pic_path_list);
		viewPager.setAdapter(adapter);
		viewPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int position) {
//				setPagerTitle(position);

				nowUrl = pic_path_list.get(position);
				System.out.println("ws---int = "+position+"    "+nowUrl);
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {

			}
		});

	}

	/**
	 * 设置标题
	 * 
	 * @param index
	 */
	private void setPagerTitle(int index) {
		setTitle(title + " " + (index + 1) + "/" + pic_path_list.size());
	}

	private class ImagePagerAdapter extends PagerAdapter {
		private boolean dataSetChanged;

		@Override
		public int getCount() {
			if (pic_path_list == null)
				return 0;
			return pic_path_list.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			ImageView iv = (ImageView) object;
			if (iv != null) {
				container.removeView(iv);
				iv.setImageBitmap(null);
			}
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			if (position >= pic_path_list.size())
//				return super.instantiateItem(container, position);
			L.i("instantiateItem position=" + position);
			ImageView imageView = new ImageView(ImagePagerActivity.this);
			imageView.setImageBitmap(BitmapFactory.decodeFile(pic_path_list
					.get(position)));
			container.addView(imageView);
			return imageView;
		}

		@Override
		public void notifyDataSetChanged() {
			dataSetChanged = true;
			super.notifyDataSetChanged();
		}

		@Override
		public int getItemPosition(Object object) {
			if (dataSetChanged) {
				dataSetChanged = false;
				return POSITION_NONE;
			}
			return super.getItemPosition(object);
		}

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_back:// 后退
			if (modified) {// 有修改
				result0k();// 返回数据
			} else {
				setResult(RESULT_CANCELED);
			}
			finish();
			break;

		case R.id.update_db:// 编辑 删除
			showDelDialog();
			break;
		}
	}

	private void result0k() {
		Intent data = new Intent();
		data.putStringArrayListExtra("pic_path_list", pic_path_list);
		setResult(RESULT_OK, data);
	}

	private ZAlertDialog.Builder delDialog;
	private DialogInterface.OnClickListener delDialogListener = new DialogInterface.OnClickListener() {

		@Override
		public void onClick(DialogInterface dialog, int which) {
			if (which == DialogInterface.BUTTON_POSITIVE) {

				pic_path_list.remove(nowUrl);
				adapter.getmArrayData().remove(nowUrl);

				adapter.notifyDataSetChanged();
				setPagerTitle(viewPager.getCurrentItem());
				modified = true;
				if (pic_path_list.size() == 0) {
					result0k();
					finish();
				}
			}
		}
	};

	/**
	 * 显示删除对话框
	 */
	private void showDelDialog() {
		if (delDialog == null) {
			delDialog = new ZAlertDialog.Builder(this);
			delDialog.setTitle(R.string.delete_photo);
			delDialog.setMessage(R.string.delete_photo_message);
			delDialog.setPositiveButton(R.string.ok, delDialogListener);
			delDialog.setNegativeButton(R.string.cancel, null);
		}
		delDialog.show();
	}

}
